package com.chen.seckill.config;

import com.chen.seckill.pojo.User;
import com.chen.seckill.service.IUserService;
import com.chen.seckill.utils.CookieUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.core.MethodParameter;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 自定义参数校验
 */
@Component
public class UserArgumentResolver implements HandlerMethodArgumentResolver {

    @Resource
    private IUserService userService;

    /**
     * 是否是符合要求的参数类型
     * @param parameter
     * @return
     */
    @Override
    public boolean supportsParameter(MethodParameter parameter) {
        System.out.println(parameter.getParameter());
        Class<?> parameterType = parameter.getParameterType();
        return parameterType== User.class;
    }

    /**
     * 只有当supportsParameter方法返回true时
     * 才会执行下面的方法（resolveArgument）
     * resolveArgument方法为自定义参数校验的具体实现
     * @param parameter
     * @param mavContainer
     * @param webRequest
     * @param binderFactory
     * @return
     * @throws Exception
     */
    @Override
    public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {

        return UserContext.getUser();
    }
}
